﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SilverlightMessageBoxLibrary;
using System.Collections.ObjectModel;
using System.Net.Browser;
using System.Windows.Media.Imaging;
using System.IO;
namespace CommunitySystem
{
    public partial class MenuManage : UserControl
    {
        private DataService.DataServiceClient client = null;
        public MenuManage()
        {
            InitializeComponent();
            client = new DataService.DataServiceClient();
            client.GetMenuByContidtionAsync("1=1");
            //client.GetDictionarysCompleted += (sender, e) =>
            //{
            //    var result = e.Result;
            //};
            //client.GetDictionarysAsync();
            client.GetMenuByContidtionCompleted += new EventHandler<DataService.GetMenuByContidtionCompletedEventArgs>(client_GetMenuByContidtionCompleted);
           
        }

        private TreeViewItem selectitem = null;
        void client_GetMenuByContidtionCompleted(object sender, DataService.GetMenuByContidtionCompletedEventArgs e)
        {
            menuTree.Items.Clear();
            ObservableCollection<CommunitySystem.DataService.MenuItem> items = e.Result;
            foreach (var i in items.Where(c => c.PID == 0))
            {
                TreeViewItem item = new TreeViewItem();
                //item.SetBinding(TreeViewItem.HeaderProperty, new System.Windows.Data.Binding() { Path = new PropertyPath("Title")});
                StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal,  HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch, VerticalAlignment = System.Windows.VerticalAlignment.Stretch };
                if (i.icon != null && i.icon.Length > 0)
                {
                    Image img = new Image() { HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch, Height =16, Width =16 ,Stretch= Stretch.Fill,VerticalAlignment = System.Windows.VerticalAlignment.Stretch };
                    BitmapImage b = new BitmapImage();
                    Stream mm = new MemoryStream();
                    mm.Write(i.icon, 0, i.icon.Length);
                    b.SetSource(mm);
                    img.Source = b;
                    using (MemoryStream stream = new MemoryStream(i.icon))
                    {
                        stream.Seek(0, SeekOrigin.Begin);
                        BitmapImage b2 = new BitmapImage();
                        b2.SetSource(stream);
                        img.Source = b2;
                    }
                    sp.Children.Add(img);
                }
                sp.Children.Add(new TextBlock() { Text = i.Title + "[是否默认打开:" + (i.IsDefault ? "是" : "否") + "][排序:" + i.Index + "]:" + i.Target, FontSize = 12, Margin = new Thickness(4, 0, 0, 0) });
                item.Header = sp;
                item.DataContext = i;
                AddChildNode(item, i,items);
                if (i.ID == SelectID)
                {
                    item.IsExpanded = true;
                }
                menuTree.Items.Add(item);
            }
            
           
        }

        void AddChildNode(TreeViewItem pitem, CommunitySystem.DataService.MenuItem pmenu,ObservableCollection<CommunitySystem.DataService.MenuItem> source)
        {
            foreach (var i in source.Where(c => c.PID == pmenu.ID))
            {
                TreeViewItem item = new TreeViewItem();
                //item.SetBinding(TreeViewItem.HeaderProperty, new System.Windows.Data.Binding() { Path = new PropertyPath("Title") });
                StackPanel sp = new StackPanel() { Orientation = Orientation.Horizontal,   HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch, VerticalAlignment = System.Windows.VerticalAlignment.Stretch };
                if (i.icon != null && i.icon.Length > 0)
                {
                    Image img = new Image() { Height = 16, Width = 16, };
                    BitmapImage b = new BitmapImage();
                    Stream mm = new MemoryStream();
                    mm.Write(i.icon, 0, i.icon.Length);
                    b.SetSource(mm);
                    img.Source = b;
                    sp.Children.Add(img);
                }
                sp.Children.Add(new TextBlock() { Text = i.Title + "[是否默认打开:" + (i.IsDefault ? "是" : "否") + "][排序:" + i.Index + "]:" + i.Target, FontSize = 12, Margin = new Thickness(4, 0, 0, 0) });
                item.Header = sp;
                item.DataContext = i;
                AddChildNode(item, i, source);
                if (i.ID == SelectID)
                {
                    item.IsExpanded = true;
                }
                pitem.Items.Add(item);
            }
        }

        private void NewData_Click(object sender, RoutedEventArgs e)
        {
            NewMenuItemWindow window = null;
            if (menuTree.SelectedItem != null)
            {
                CommunitySystem.DataService.MenuItem item = (menuTree.SelectedItem as TreeViewItem).DataContext as CommunitySystem.DataService.MenuItem;
                window = new NewMenuItemWindow(null, item, "新增菜单项");
                window.Show();
            }
            else
            {
                //CommunitySystem.DataService.MenuItem item = (menuTree.SelectedItem as TreeViewItem).DataContext as CommunitySystem.DataService.MenuItem;
                window = new NewMenuItemWindow(null, null, "新增菜单项");
                window.Show();
            }
            window.Closed += new EventHandler(window_Closed);
            //window.Title = "新增菜单项";

        }

        void window_Closed(object sender, EventArgs e)
        {
            NewMenuItemWindow window = sender as NewMenuItemWindow;
            if (window != null && window.DialogResult == true)
            {
                client.GetMenuByContidtionAsync("1=1");
            }
        }

        private void EditData_Click(object sender, RoutedEventArgs e)
        {
            if (menuTree.SelectedItem != null)
            {
                CommunitySystem.DataService.MenuItem item = (menuTree.SelectedItem as TreeViewItem).DataContext as CommunitySystem.DataService.MenuItem;
                NewMenuItemWindow window = new NewMenuItemWindow(item, null, "编辑菜单项");
                window.Show();
                window.Closed += new EventHandler(window_Closed);
            }
            else
            {
                MessageBox.Show("请选中编辑项!");
            }
        }
        private int SelectID = 0;
        private void DeleteData_Click(object sender, RoutedEventArgs e)
        {
            if (menuTree.SelectedItem == null)
            {
                MessageBox.Show("请选中删除项");
                return;
            }

            CustomMessage confirmMessage = new CustomMessage("确认删除选中项?", CustomMessage.MessageType.Confirm);

            confirmMessage.OKButton.Click -= new RoutedEventHandler(OKButton_Click);
            confirmMessage.OKButton.Click += new RoutedEventHandler(OKButton_Click);

            confirmMessage.Show();

        }

        void OKButton_Click(object sender, RoutedEventArgs e)
        {
            CommunitySystem.DataService.MenuItem item = (menuTree.SelectedItem as TreeViewItem).DataContext as CommunitySystem.DataService.MenuItem;
            client.DeleteMenuItemAsync(item.ID);
            client.DeleteMenuItemCompleted -= new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(client_DeleteMenuItemCompleted);
            client.DeleteMenuItemCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(client_DeleteMenuItemCompleted);
        }

        void client_DeleteMenuItemCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            Message.InfoMessage("删除成功!");
            client.GetMenuByContidtionAsync("1=1");

        }


    }
}
